import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QApplication,QMainWindow,QWidget,QTextEdit,QVBoxLayout,QPushButton,QStyleFactory,QTableWidget,QTableWidgetItem)
from PyQt5 import QtGui
from pwm import Ui_MainWindow
from decimal import Decimal,localcontext

class MyMainWindow(QMainWindow,Ui_MainWindow,QWidget):
    def __init__(self,parent = None):
        super(MyMainWindow,self).__init__(parent)
        self.setupUi(self)

        self.setWindowIcon(QIcon('./ic.cio'))
        self.setWindowTitle("PWM计算助手")
        self.ok_1.clicked.connect(self.CalculateHz)
        self.ok_2.clicked.connect(self.CalculateArr)
        self.ok_3.clicked.connect(self.CalculatePsc)
        self.clear_1.clicked.connect(self.ClearHz)
        self.clear_2.clicked.connect(self.CleraArr)
        self.clear_3.clicked.connect(self.ClearPsc)

        Tim = ["TIM1","TIM2","TIM3","TIM4","TIM5","TIM8","TIM9","TIM10","TIM11","TIM12","TIM13","TIM14"]
        self.TIM1 = ["TIM2","TIM3","TIM4","TIM5","TIM12","TIM13","TIM14"]
        self.TIM2 = ["TIM1","TIM8","TIM9","TIM10","TIM11"]
        self.tim_select_1.addItems(Tim)
        self.tim_select_2.addItems(Tim)
        self.tim_select_3.addItems(Tim)

        unit = ["Hz", "K", "M"]
        self.danwei_1.addItems(unit)
        self.danwei_2.addItems(unit)
        self.danwei_3.addItems(unit)


    def CalculateHz(self):
        try:
            self.dis_1.clear()
            psc = int(self.psc_1.text())
            psc = Decimal(psc)
            arr = int(self.arr_1.text())
            arr = Decimal(arr)
            st = ""+self.tim_select_1.currentText()+"_PWM_Init("+"%s"%str(arr)+"-1,"+"%s"%str(psc)+"-1);"
            if self.tim_select_1.currentText() in self.TIM1:
                Tim = 84
            else:
                Tim = 168
            with localcontext() as ctx:
                ctx.prec = 5     #小数位数-2
                result = Tim*1000000/(psc*arr)
                print(result)
            if self.danwei_1.currentText() == 'M':
                result = result/1000000
                self.result_1.setText(str(result)+"  MHz")
            elif self.danwei_1.currentText() == 'K':
                result = result/1000
                self.result_1.setText(str(result) + "  KHz")
            else:
                self.result_1.setText(str(result) + "  Hz")
            self.dis_1.append(st)
        except Exception as err:
            print(err)

        pass

    def CalculateArr(self):
        try:
            self.dis_2.clear()
            hz = int(self.hz_input.text())
            hz = Decimal(hz)
            psc = int(self.psc_2.text())
            psc = Decimal(psc)
            if self.tim_select_2.currentText() in self.TIM1:
                Tim = 84
            else:
                Tim = 168
            if self.danwei_2.currentText() =='M':
                with localcontext() as ctx:
                    ctx.prec = 5  # 小数位数-2
                    hz = hz*1000000
                    result = Tim*1000000/(psc*hz)
                    result = float(result)
                    self.result_2.setText(str(result))
            elif self.danwei_2.currentText() =='K':
                with localcontext() as ctx:
                    ctx.prec = 5  # 小数位数-2
                    hz = hz * 1000
                    result = Tim*1000000 / (psc * hz)
                    self.result_2.setText(str(result))
            else:
                with localcontext() as ctx:
                    ctx.prec = 5  # 小数位数-2
                    result = Tim*1000000 / (psc * hz)
                    #result = float(result)
                    self.result_2.setText(str(result))
            st = "" + self.tim_select_2.currentText() + "_PWM_Init(" + "%s" % str(int(result)) + "-1," + "%s" % str(
                psc) + "-1);"
            self.dis_2.append(st)
        except Exception as err:
            print(err)


        pass

    def CalculatePsc(self):
        try:
            self.dis_3.clear()
            hz = int(self.hz_input_2.text())
            hz = Decimal(hz)
            arr = int(self.arr_2.text())
            arr= Decimal(arr)
            if self.tim_select_3.currentText() in self.TIM1:
                Tim = 84
            else:
                Tim = 168
            if self.danwei_3.currentText() == 'M':
                with localcontext() as ctx:
                    ctx.prec = 5  # 小数位数-2
                    hz = hz * 1000000
                    result = Tim * 1000000 / (arr * hz)
                    #result = float(result)
                    self.result_3.setText(str(result))
            elif self.danwei_3.currentText() == 'K':
                with localcontext() as ctx:
                    ctx.prec = 5  # 小数位数-2
                    hz = hz * 1000
                    result = Tim * 1000000 / (arr * hz)
                    self.result_3.setText(str(result))
            else:
                with localcontext() as ctx:
                    ctx.prec = 5  # 小数位数-2
                    result = Tim * 1000000 / (arr * hz)
                    #result = float(result)
                    self.result_3.setText(str(result))
            st = "" + self.tim_select_3.currentText() + "_PWM_Init(" + "%s" % str(arr) + "-1," + "%s" % str(int(result)) + "-1);"
            self.dis_3.append(st)
        except Exception as err:
            print(err)


    def ClearHz(self):
        self.psc_1.clear()
        self.arr_1.clear()
        self.result_1.clear()
        self.dis_1.clear()

        pass

    def CleraArr(self):
        self.hz_input.clear()
        self.psc_2.clear()
        self.result_2.clear()
        self.dis_2.clear()

        pass

    def ClearPsc(self):
        self.hz_input_2.clear()
        self.arr_2.clear()
        self.result_3.clear()
        self.dis_3.clear()

        pass




if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyMainWindow()
    myWin.show()
    sys.exit(app.exec_())